<?php


 $pdo = new PDO("mysql:host=localhost;dbname=hospital","root","tailiyan88"); 
 
$json_string = file_get_contents("a.json");
    
$obj=json_decode($json_string,true);
if (!is_array($obj)) die('no successful');

	foreach($obj as $v){
		$province=$v['label'];
		$key=$v['key'];
		$city="";
		$area="";
		if(!empty($v["children"])&& is_array($v["children"]))
		{
			foreach($v["children"] as $c)
			{
				$city=$c['label'];
				$key=$c['key'];
				if(!empty($c["children"])&& is_array($c["children"])) 
		         {
					 foreach($c["children"] as $s)
						{
							if(!empty($s["children"])&& is_array($s["children"])) 
		                      {
								 foreach($s["children"] as $ss)
								 {
									$areat=$ss['label'];
									$keyt=$ss['key'];
									
									$sql="SELECT hospital.danwei,areas.lable,areas.`key` FROM hospital INNER JOIN areas ON hospital.`key` = areas.`key` where hospital.`key`='$keyt'";
									
									$result=$pdo->query($sql);
									foreach($result as $rowt){
									  $pdo->exec("insert into danweiku(`province`,`city`,`area`,`danwei`) values('$province','$city','$areat','".$rowt['danwei']."')");
									} 
								 }								 
								  
							  }
							  else{
								$areat=$s['label'];
								$keyt=$s['key'];
								
								$sql="SELECT hospital.danwei,areas.lable,areas.`key` FROM hospital INNER JOIN areas ON hospital.`key` = areas.`key` where hospital.`key`='$keyt'";
								
								$result=$pdo->query($sql);
								foreach($result as $rowt){
								  $pdo->exec("insert into danweiku(`province`,`city`,`area`,`danwei`) values('$province','$city','$areat','".$rowt['danwei']."')");
								}
							
							}
						}
				
				 }
			}
		 
		}else{
			$result=$pdo->query("SELECT hospital.danwei,areas.lable,areas.`key` FROM hospital INNER JOIN areas ON hospital.`key` = areas.`key` where hospital.`key`='$key'");
			
			foreach($result as $row){
			$pdo->exec("insert into danweiku(`province`,`city`,`area`,`danwei`) values('$province','','','".$row['danwei']."')");
			 sleep(1);
			}
		}
			
	}
	
	